<?php
class  OrderMarketSubsidy extends BaseModel
{
	public $res;
	public $key;
	public function getSource()
	{
		return "order_market_subsidy";
	}

	public function initialize()
	{
		parent::initialize();
	}


	/*
	 添加用户分账订单
	 */
	public function add_subsidy($time,$money){
		$time=explode(' ',$time)[0];
		foreach($money as $k =>$v){
		$condition['conditions']="user_id = '{$k}' and add_day='{$time}'";
		$res=$this->findFirst($condition);
		if($res){
			$res->updated_at=date('Y-m-d H:i:s');
		}else{
			$res=new OrderMarketSubsidy();
			$res->created_at=date('Y-m-d H:i:s');
		}
		$res->user_id=$k;
			$res->money=$v;
			$res->add_day=$time;
			$res->save();
		}
	}
	//获取用户的市场补贴
	public function sum_money($user_id,$start_end_time){
		foreach($start_end_time as $k =>$v){
		$start_end_time[$k]=explode(" ",$v)[0];
		}
		$condition['conditions']="user_id = '{$user_id}' and add_day >= '{$start_end_time['start_at']}' and add_day <= '{$start_end_time['end_at']}'";
		$condition['columns']='sum(money) as money';
		$res=$this->findFirst($condition);
		if($res){
		return doubleval($res->money);
		}
		return 0;
	}
	/*
	 汇总
	 */
	public function sum_moneys($user_id){
		$condition['conditions']="user_id = '{$user_id}' and is_settled = 0";
		$condition['columns']='sum(money) as money';
		return doubleval($this->findFirst($condition)->money);

	}

}


